package com.etc.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import com.etc.dao.ManagerDao;
import com.etc.entity.Person;
import com.etc.service.ManagerService;
import com.etc.service.PersonService;
import com.etc.service.impl.ManagerServiceImpl;
import com.etc.service.impl.PersonServiceImpl;

public class LoginServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.获取参数
		String account = request.getParameter("account");
		String pwd = request.getParameter("pwd");
		// 调用服务
		PersonService p = new PersonServiceImpl();
		Person person = new Person();
		ManagerService m = new ManagerServiceImpl();

		// 管理员
		if (m.checkAccount(account, pwd)) {
			request.setAttribute("admin", "admin");
			request.getRequestDispatcher("/manager.jsp").forward(request,
					response);
		} else {
			if (p.check(account, pwd))// 账号密码正确
			{
				person = p.findByAccount(account);
				person.setState(1);
				// 设置域
				HttpSession session = request.getSession();
				session.setAttribute("person", person);
				// 4. 页面的跳转 相当于访问 http://localhost:8080/myweb/index.jsp
				request.getRequestDispatcher("/menu.jsp").forward(request,
						response);
			}
		}
	}

}
